package com.lg.baselibrary.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.lg.baselibrary.R;
import com.lg.baselibrary.listener.OnTabItemSelectListener;
import com.lg.baselibrary.widget.BottomTabViewForViewPager;

/**
 * Created by LiGang on 2017/5/29.
 * 用于底部Tabs+ViewPager+Fragment的app界面主架构
 * 支持底部tabs中间有个突出的CenterView，需要手动重写setCenterView()方法，在方法里调用
 * View setCenterView(int centerViewLayout, View.OnClickListener clickListener)
 */

public abstract class BaseBottomViewPagerTabActivity extends BaseBottomTabActivity implements OnTabItemSelectListener {
    ViewPager viewPager;
    FragmentPagerAdapter adapter;
    @Override
    protected int provideContentViewLayout() {
        return R.layout.activity_base_bottom_viewpagertab;
    }

    @Override
    protected void initView() {
        super.initView();
        viewPager = findView(R.id.viewPager);

        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return getFragments().get(position);
            }

            @Override
            public int getCount() {
                return getFragments().size();
            }
        };

        viewPager.setAdapter(adapter);
        setCenterView();
        if (getCenterView() == null){
            bottomTabView.setTabItemViews(getTabViews());
        }else {
            bottomTabView.setTabItemViews(getTabViews(), getCenterView());
        }
        bottomTabView.setOnTabItemSelectListener(this);
        ((BottomTabViewForViewPager)bottomTabView).setUpWithViewPager(viewPager);
    }

    @Override
    public void onTabItemSelect(int position) {
        viewPager.setCurrentItem(position);
    }

    @Override
    public boolean onInterceptTabItemSelect(int position) {
        return false;
    }
}
